package com.es.util.common;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.es.main.LoginUserBeen;
import com.es.util.db.Dao;

public class CommonBiz {
	/**
	 * 为了验证权限，增加当权登录用户
	 * 		总部	系统管理员 事业部管理员	rtm				ctm			其他
	 *	区域 all  all	 all	    自己				自己			自己       
	 *  省份	all	 all     all		all 			自己 		自己
	 *  城市 all  all	 all		all 			自己 		自己
	 *  角色 all  all  RTM,CTM,其他和自己 ctm，其他和自己 其他和自己	自己	     	role
	 *  事业部all  all    自己		自己				自己			自己			shiyebu
	 */
	LoginUserBeen userBean = null;
	String role = null;
	String shiyebu = null;
	String area = null;
	
	public CommonBiz(LoginUserBeen userBean){		
		if(userBean==null) return;
		this.userBean = userBean;
		this.role = userBean.getLoginRole();
		this.shiyebu = userBean.getShiyebu();
		this.area = userBean.getEara();
	}
	
	public ArrayList<CommonBean> getCommonList(String type)throws Exception {

		ArrayList<String> query = new ArrayList<String>();
		ArrayList<CommonBean> result = new ArrayList<CommonBean>();
		if(userBean==null) return result;
		query.add(type);
		Dao dao = new Dao();
		String sql = "select common_value,common_code from hrm_common where common_type=? ";
		if("shiyebu".equals(type)){//事业部
			if("zongbu1".indexOf(role)>=0){
			}else{
				sql = "select common_value,common_code from hrm_common where common_type=?  " +
						"and common_code  = '"+shiyebu+"'";
			}
		}
		if("role".equals(type)){//角色
			if("zongbu1".indexOf(role)>=0){
			}else if("shiyebu".equals(role)){
				sql = "select common_value,common_code from hrm_common where common_type=?  " +
						"and common_code not in('1','zongbu','shiyebu')";
			}else if("RTM".equals(role)){
				sql = "select common_value,common_code from hrm_common where common_type=?  " +
				"and common_code not in('1','zongbu','shiyebu','RTM')";
			}else {//CTM&&其他
				sql = "select common_value,common_code from hrm_common where common_type=?  " +
				"and common_code not in('1','zongbu','shiyebu','RTM','CTM')";
			}
		}
		try {
			ResultSet resultSet = dao.Select(sql,query);
			while(resultSet.next()){
				CommonBean bean = new CommonBean();
				bean.setCommonCode(resultSet.getString("common_code"));
				bean.setCommonValue(resultSet.getString("common_value"));
				result.add(bean);
			}
		} finally{
			dao.close();
		}
		return result;
	}
	
	public ArrayList<AreaBen> getAreaList()throws Exception {

		ArrayList<AreaBen> result = new ArrayList<AreaBen>();
		Dao dao = new Dao();
		String sql = "select area_code,area_value from hrm_area order by area_code";
		if("zongbu1shiyebu".indexOf(role)<0){
			sql = "select area_code,area_value from hrm_area where area_value =  '"+area +"'";
		}

		try {
			ResultSet resultSet = dao.Select(sql);
			while(resultSet.next()){
				AreaBen bean = new AreaBen();
				bean.setAreaCode(resultSet.getString("area_code"));
				bean.setAreaValue(resultSet.getString("area_value"));
				result.add(bean);
			}
		} finally{
			dao.close();
		}
		return result;
	}
	
	public ArrayList<AreaBen> getAreaListExcept(String eara_code)throws Exception {

		ArrayList<AreaBen> result = new ArrayList<AreaBen>();
		Dao dao = new Dao();
		String sql = "select area_code,area_value from hrm_area  where area_code !='"+eara_code+"' order by area_code";
		if("zongbu1shiyebu".indexOf(role)<0){
			sql = "select area_code,area_value from hrm_area where area_value =  '"+area +"'";
		}

		try {
			ResultSet resultSet = dao.Select(sql);
			while(resultSet.next()){
				AreaBen bean = new AreaBen();
				bean.setAreaCode(resultSet.getString("area_code"));
				bean.setAreaValue(resultSet.getString("area_value"));
				result.add(bean);
			}
		} finally{
			dao.close();
		}
		return result;
	}
	
		
	public ArrayList<CityBean> getCityList(String type)throws Exception {

		ArrayList<CityBean> result = new ArrayList<CityBean>();
		ArrayList<String> query = new ArrayList<String>();
		query.add(type);
		Dao dao = new Dao();
		String sql = "select city_code,city_name,city_level,province_code,citys_code from hrm_city where province_code=? order by province_code";
		try { 
			ResultSet resultSet = dao.Select(sql,query);
			while(resultSet.next()){
				CityBean bean = new CityBean();
				bean.setCityCode(resultSet.getString("city_code"));
				bean.setCityLevel(resultSet.getString("city_level"));
				bean.setCityName(resultSet.getString("city_name"));
				bean.setProvinceCode(resultSet.getString("province_code"));
				bean.setCitysCode(resultSet.getString("citys_code"));
				result.add(bean);
			}
		} finally{
			dao.close();
		}
		return result;
	}
	
	public ArrayList<ZhuanquBean> getZhuanquList(String type)throws Exception {

		ArrayList<ZhuanquBean> result = new ArrayList<ZhuanquBean>();
		ArrayList<String> query = new ArrayList<String>();
		query.add(type);
		Dao dao = new Dao();
		String sql = "select daqu,zhuanqu from hrm_daqu_zhuanqu where daqu=? order by daqu_zhuanqu_id";
		try { 
			ResultSet resultSet = dao.Select(sql,query);
			while(resultSet.next()){
				ZhuanquBean bean = new ZhuanquBean();
				bean.setDaquan(resultSet.getString("daqu"));
				bean.setZhuanqu(resultSet.getString("zhuanqu"));
				result.add(bean);
			}
		} finally{
			dao.close();
		}
		return result;
	}
	
	public ArrayList<ProvinceBean> getProvinceList(String type)throws Exception {

		ArrayList<ProvinceBean> result = new ArrayList<ProvinceBean>();
		ArrayList<String> query = new ArrayList<String>();
		query.add(type);
		Dao dao = new Dao();
		String sql = "select province_code,province_name,area_code from hrm_province where area_code=? order by area_code";
		try {
			ResultSet resultSet = dao.Select(sql,query);
			while(resultSet.next()){
				ProvinceBean bean = new ProvinceBean();
				bean.setAreaCode(resultSet.getString("area_code"));
				bean.setProvinceCode(resultSet.getString("province_code"));
				bean.setProvinceName(resultSet.getString("province_name"));
				result.add(bean);
			}
		} finally{
			dao.close();
		}
		return result;
	}	
	
	public boolean CheckLoginId(String loginId)throws Exception {

		if(loginId.equals(""))
			return false;
		ArrayList<String> query = new ArrayList<String>();
		query.add(loginId);
		Dao dao = new Dao();
		String sql = "select emp_login_id from hrm_emp_info where del_flag='N' and ltrim(rtrim(emp_login_id))=?";
		try {
			ResultSet resultSet = dao.Select(sql,query);
			if(resultSet.next()){
				return true;
			}
		} finally{
			dao.close();
		}
		return false;
	}
	
}
